﻿using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace CSharp.Common.Http
{
    public class CMD
    {
        public static bool CmdPing(string strIp)
        {
            string str;
            bool flag = false;
            Process process = new Process
            {
                StartInfo = { FileName = "cmd.exe", UseShellExecute = false, RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true }
            };
            process.Start();
            process.StandardInput.WriteLine("ping -n 1 " + strIp);
            process.StandardInput.WriteLine("exit");
            string str2 = process.StandardOutput.ReadToEnd();
            if (str2.IndexOf("(0% loss)") != -1)
            {
                str = "Connected";
                flag = true;
            }
            else if (str2.IndexOf("Destination host unreachable.") != -1)
            {
                str = "Destination host unreachable";
            }
            else if (str2.IndexOf("Request timed out.") != -1)
            {
                str = "Request timed out";
            }
            else if (str2.IndexOf("Unknown host") != -1)
            {
                str = "Unknown host";
            }
            else
            {
                str = str2;
            }
            process.Close();
            return flag;
        }

        [DllImport("wininet.dll")]
        private static extern bool InternetGetConnectedState(out int Description, int ReservedValue);
        public static bool IsConnectedToInternet()
        {
            int num;
            return InternetGetConnectedState(out num, 0);
        }
    }
}

